﻿using MediatR;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Domain.Entity;
using RBAC_Uthorization.Infrstructre.Interface;
using RBAC_Uthorization.Read.API.Application.Command.UserInfoCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Response.User;

namespace RBAC_Uthorization.Read.API.Application.CommandHandlers.UserInfoHandler
{
    public class UserSelectShowCommandHandlers : IRequestHandler<UserSelectShowCommand, ApiResult<List<UserSelectShow>>>
    {
        private readonly IUserInfoRepository _userInfoRepository;
        private readonly ILogger<UserSelectShowCommandHandlers> _logger;

        public UserSelectShowCommandHandlers(IUserInfoRepository userInfoRepository, ILogger<UserSelectShowCommandHandlers> logger)
        {
            _userInfoRepository = userInfoRepository;
            _logger = logger;
        }

        public async Task<ApiResult<List<UserSelectShow>>> Handle(UserSelectShowCommand request, CancellationToken cancellationToken)
        {
            try
            {
                List<sys_user> user;
                if (request.offic_id == "10086")
                {
                     user =await _userInfoRepository.GetAllAsync();
                }
                else
                {
                    user = await _userInfoRepository.GetAllAsunc(x => x.offic_id == request.offic_id&&x.del_flag==0&&x.sys_id==request.sys_id);

                }
                if (user == null)
                {
                    return new ApiResult<List<UserSelectShow>> { Code = ResultCode.NoContent, Message = "用户不存在" };
                }
                var userSelectShow = user.Select(x => new UserSelectShow
                {
                    id = x.id,
                    name = x.name,
                }).ToList();
                return new ApiResult<List<UserSelectShow>> { Data = userSelectShow };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户信息失败");
                return new ApiResult<List<UserSelectShow>> { Code = ResultCode.Fail, Message = "获取用户信息失败" };
            }
        }
    }
}
